import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:namewava/jc_tabbar.dart';

import 'package:namewava/pages/launch/widgets/jc_login_pop.dart';
import 'package:namewava/utils/jc_cache.dart';
import 'package:namewava/utils/jc_global.dart';
import 'package:namewava/utils/jc_image_path.dart';
import 'package:namewava/utils/jc_text_style.dart';
import 'package:namewava/utils/js_request.dart';
import 'package:notification_centre/notification_centre.dart';
import 'package:sign_in_with_apple/sign_in_with_apple.dart';

class JcLoginBtn extends StatelessWidget {
  const JcLoginBtn(
      {super.key,
      this.butnTitle,
      this.top = 0,
      this.imageIcon,
      this.loginType = 0,
      required this.isArgger,
      required this.argeeEvents});
  final String? butnTitle;
  final double? top;
  final String? imageIcon;
  final int? loginType;
  final bool isArgger;
  final Function argeeEvents;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Container(
        margin: EdgeInsets.only(left: 30.w, right: 30.w, top: top ?? 0),
        height: 50.h,
        padding: EdgeInsets.only(left: 33.w, right: 33.w),
        decoration: const BoxDecoration(
            image: DecorationImage(
                image: AssetImage(JcImagePath.jcWavaItem), fit: BoxFit.fill)),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Text(
              butnTitle ?? '',
              style: JcTextStyle.sourceKeynoteartHansTextStyle(
                  18.sp, Colors.white),
            ),
            Image.asset(imageIcon ?? '')
          ],
        ),
      ),
      onTap: () {
        // NotificationCenter().postNotification('loninEvents',data: {'a':'123'});
        //   EasyLoading.show(maskType: EasyLoadingMaskType.black);
        //   Future.delayed(Duration(seconds: 10),(){
        //     EasyLoading.dismiss();
        //   });
        if (isArgger) {
          _btnTap();
        } else {
          Get.dialog(JcLoginPop(
            backEvents: () {
              argeeEvents();
              _btnTap();
            },
          ));
        }
      },
    );
  }

  _btnTap() async {
    if (loginType == 0) {
      JcGlobal.of(navigatorKey.currentContext!)
          .logReport({'page': 'quicklogin'});
      JsRequest.appFastLogin(success: (data) {
        JcGlobal.of(navigatorKey.currentContext!)
            .logReport({'page': 'loginsuccess'});
        var token = data['token'];
        JcCache.getInstance().setData('wavaToken', token);
        JcCache.getInstance().setData('wavaIsLogin', true);
        JcCache.getInstance().setData('isFirstRegister', data['isFirstRegister']);
        JcCache.getInstance().setData('wavaUserId', data['userInfo']['userId']);
        NotificationCenter().postNotification('wavaLoninEvents');
        Get.back();
        // print(token);
      });
    } else {
      JcGlobal.of(navigatorKey.currentContext!)
          .logReport({'page': 'applelogin'});

      final appleLogin = await SignInWithApple.getAppleIDCredential(
        scopes: [
          AppleIDAuthorizationScopes.fullName,
          AppleIDAuthorizationScopes.email,
        ],
      );

      if (appleLogin.identityToken != null) {
        JsRequest.appleLogin(
            identityToken: appleLogin.identityToken,
            success: (data) {
              var token = data['token'];
              JcCache.getInstance().setData('wavaToken', token);
              JcCache.getInstance().setData('wavaIsLogin', true);
              JcCache.getInstance()
                  .setData('wavaUserId', data['userInfo']['userId']);
                   JcCache.getInstance().setData('isFirstRegister', data['isFirstRegister']);
              NotificationCenter().postNotification('wavaLoninEvents');
              Get.back();
              // print(token);
            });
      } else {
        EasyLoading.showError('Get Apple Info Fail');
      }
    }
  }
}
